package com.wlw.admin.openeye.ui.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import com.wlw.admin.openeye.R
import com.wlw.admin.openeye.base.BaseFragment
import com.wlw.admin.openeye.base.BaseFragmentAdapter
import com.wlw.admin.openeye.mvp.contract.HotTabContract
import com.wlw.admin.openeye.mvp.model.bean.TabInfoBean
import com.wlw.admin.openeye.mvp.presenter.HotTabPresenter
import com.wlw.admin.openeye.net.exception.ErrorStatus
import com.wlw.admin.openeye.showToast
import com.wlw.admin.openeye.utils.StatusBarUtil
import kotlinx.android.synthetic.main.fragment_hot.*
import kotlinx.android.synthetic.main.fragment_toolbar.*

class HotFragment : BaseFragment(), HotTabContract.View {

    private val mPresenter by lazy {
        HotTabPresenter()
    }
    private val mTabTitleList = ArrayList<String>()
    private val mFragmentList = ArrayList<Fragment>()

    companion object {
        fun getInstance(title: String): HotFragment {
            val fragment = HotFragment()
            val bundle = Bundle()
            bundle.putString("title", title)
            fragment.arguments = bundle
            return fragment
        }
    }

    init {
        mPresenter.attachView(this)
    }

    override fun getLayoutId(): Int = R.layout.fragment_hot

    override fun lazyLoad() {
        mPresenter.getTabInfo()
    }

    override fun initView() {
        mLayoutStatusView = multipleStatusView
        activity?.let {
            StatusBarUtil.darkMode(it)
            StatusBarUtil.setPaddingSmart(it, toolbar)
        }
    }

    override fun setTabInfo(tabInfoBean: TabInfoBean) {
        mLayoutStatusView?.showContent()

        tabInfoBean.tabInfo.tabList.mapTo(mTabTitleList) {
            it.name
        }
        tabInfoBean.tabInfo.tabList.mapTo(mFragmentList) {
            RankFragment.getInstance(it.apiUrl)
        }
        viewpager.adapter = BaseFragmentAdapter(mFragmentList, mTabTitleList, childFragmentManager)
        tabLayout.setupWithViewPager(viewpager)
    }

    override fun showError(errorMsg: String, errorCOde: Int) {
        showToast(errorMsg)
        if (errorCOde == ErrorStatus.NETWORK_ERROR)
            multipleStatusView.showNoNetwork()
        else
            multipleStatusView.showError()
    }

    override fun showLoading() {
        mLayoutStatusView?.showLoading()
    }

    override fun dismissLoading() {
        mLayoutStatusView?.showContent()
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }
}